﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RWSBeeldbank.Common;
using RWSBeeldbank.Data;
using Windows.ApplicationModel.Resources;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace RWSBeeldbank.DataModel
{
    /// <summary>
    /// MediaObject item data model.
    /// </summary>
    public class MediaObjectDataItem : MediaObjectDataCommon 
    {
        public MediaObjectDataItem()
            : base(String.Empty, String.Empty, String.Empty)
        {
        }

        public MediaObjectDataItem(String uniqueId, String title, String altTitle, Uri LargeImagePath, Uri TileImagePath, int filenumber)
            : base(uniqueId, title, LargeImagePath.ToString())
        {
            this._filenumber = filenumber;
            this._altTitle = altTitle;
            this._tileImagePath = TileImagePath.ToString();
            this.DirectLink = Constants.DIRECTLINKBASE + this.Id.ToString();
        }

        public MediaObjectDataItem(String uniqueId, String title, String altTitle, String imagePath, int filenumber, String description, MediaObjectDataGroup group)
            : base(uniqueId, title, imagePath)
        {
            this._filenumber = filenumber;
            this._description = description;
            this._group = group;
            this._altTitle = altTitle;
        }

        private string _altTitle = string.Empty;
        public string AltTitle
        {
            get { return this._altTitle; }
            set { this.SetProperty(ref this._altTitle, value); }
        }

        private string _author = string.Empty;
        public string Author
        {
            get { return this._author; }
            set
            {
                this.SetProperty(ref this._author, value);
                if (this.Author.Trim() != "")
                    this.CopyRight = Constants.COPYRIGHTBASE + " / " + this.Author;
            }
        }

        private string _copyright = Constants.COPYRIGHTBASE;
        public string CopyRight
        {
            get { return this._copyright; }
            set { this.SetProperty(ref this._copyright, value); }
        }

        private string _dateCreated = string.Empty;
        public string DateCreated
        {
            get { return this._dateCreated; }
            set
            {
                if (value != "1-1-1900")
                    this.SetProperty(ref this._dateCreated, value);
                else
                {
                    var resourceLoader = new ResourceLoader();

                    this.SetProperty(ref this._dateCreated, resourceLoader.GetString("DateCreatedUnknown"));
                }
            }
        }

        private string _description = string.Empty;
        public string Description
        {
            get { return this._description; }
            set { this.SetProperty(ref this._description, value); }
        }

        private string _directLink = string.Empty;
        public string DirectLink
        {
            get { return this._directLink; }
            set { this.SetProperty(ref this._directLink, value); }
        }

        private int _filenumber = 0;
        public int FileNumber
        {
            get { return this._filenumber; }
            set
            {
                this.SetProperty(ref this._filenumber, value);
            }
        }

        private int _googlezoom = 12;
        public int GoogleZoom
        {
            get { return this._googlezoom; }
            set
            {
                if (value > 0)
                    this.SetProperty(ref this._googlezoom, value);
            }
        }

        private string _gps = string.Empty;
        public string GPS
        {
            get { return this._gps; }
            set { this.SetProperty(ref this._gps, value); }
        }

        private MediaObjectDataGroup _group;
        public MediaObjectDataGroup Group
        {
            get { return this._group; }
            set { this.SetProperty(ref this._group, value); }
        }

        private int _height = 0;
        public int Height
        {
            get { return this._height; }
            set { this.SetProperty(ref this._height, value); }
        }

        private int _id = 0;
        public int Id
        {
            get { return this._id; }
            set
            {
                this.SetProperty(ref this._id, value);
            }
        }

        private string _inventoryId = string.Empty;
        public string InventoryId
        {
            get { return this._inventoryId; }
            set { this.SetProperty(ref this._inventoryId, value); }
        }

        private string _keywords = string.Empty;
        public string Keywords
        {
            get { return this._keywords; }
            set { this.SetProperty(ref this._keywords, value); }
        }

        private double _latitude = Double.NaN;
        public double Latitude
        {
            get { return this._latitude; }
            set { this.SetProperty(ref this._latitude, value); }
        }

        private string _location = string.Empty;
        public string Location
        {
            get { return this._location; }
            set { this.SetProperty(ref this._location, value); }
        }

        private double _longitude = Double.NaN;
        public double Longitude
        {
            get { return this._longitude; }
            set { this.SetProperty(ref this._longitude, value); }
        }

        private string _province = string.Empty;
        public string Province
        {
            get { return this._province; }
            set { this.SetProperty(ref this._province, value); }
        }

        private string _themes = string.Empty;
        public string Themes
        {
            get { return this._themes; }
            set { this.SetProperty(ref this._themes, value); }
        }


        private string _rating = string.Empty;
        public string Rating
        {
            get { return this._rating; }
            set { this.SetProperty(ref this._rating, value); }
        }


        private int _typeid = 0;
        public int TypeId
        {
            get { return this._typeid; }
            set { this.SetProperty(ref this._typeid, value); }
        }

        public string VideoFile
        {
            get { return Constants.WEBSITEBASE + Constants.VIDEO_URL + (FileNumber / 150).ToString() + "/" + FileNumber.ToString() + Constants.VIDEO_EXTENSION; }
        }

        private string _videoFormat = string.Empty;
        public string VideoFormat
        {
            get { return this._videoFormat; }
            set { this.SetProperty(ref this._videoFormat, value); }
        }

        private string _videoLength = string.Empty;
        public string VideoLength
        {
            get { return this._videoLength; }
            set { this.SetProperty(ref this._videoLength, value); }
        }

        private int _searchResultSetSize = 0;
        public int SearchResultSetSize
        {
            get { return this._searchResultSetSize; }
            set { this.SetProperty(ref this._searchResultSetSize, value); }
        }



        private ImageSource _tileImage;
        private string _tileImagePath;

        public Uri TileImagePath
        {
            get { return new Uri(MediaObjectDataCommon._baseUri, this._tileImagePath); }
        }

        public ImageSource TileImage
        {
            get
            {
                if (this._tileImage == null && this._tileImagePath != null)
                {
                    this._tileImage = new BitmapImage(new Uri(MediaObjectDataCommon._baseUri, this._tileImagePath));
                }
                return this._tileImage;
            }
            set
            {
                this._tileImagePath = null;
                this.SetProperty(ref this._tileImage, value);
            }
        }

        public void SetTileImage(String path, String typePath, string tileSize = Constants.TILE_SIZE_DIGITAL)
        {
            // path = http://beeldbank.rws.nl/MediaService/313367
            //
            // must result in
            //
            // _tileImagePath = http://beeldbank.rws.nl/MediaService/[250x250-313367] to get a smaller image of the right size
            //
            // Or we can use the preview file, which is 600 pixels wide
            // That would lead to something like this 
            // this._tileImagePath = "https://beeldbank.rws.nl/Photos/" + (_filenumber/150).ToString() + "/" + _filenumber.ToString() + "s.jpg";
            //
            // Original version to generate the tile image using the MediaService
            // this._tileImagePath = path.Replace(Constants.HTTP, Constants.HTTPS).Replace(Constants.TILE_WIDTH_SOURCE, Constants.TILE_WIDTH_TARGET1) + Constants.TILE_WIDTH_TARGET2;

            this._tileImage = null;
            
            this._tileImagePath = typePath + (_filenumber / 150).ToString() + "/" + _filenumber.ToString() + "_[" + tileSize + "].jpg";

            this.OnPropertyChanged("TileImage");
        }

        public void SetTileImageFromId(String id)
        {
            this._tileImage = null;
            this._tileImagePath = "https://beeldbank.rws.nl/Home/DisplayThumb/" + id;
            this.OnPropertyChanged("TileImage");
        }
    }
}
